const AJAX = getApp().ajax,
      $wx = getApp().$wx,
      getColor = getApp().globalData.getColor;

Page({
  data:{
    isCollectActive: false
  },

  onLoad(options){
    this.setData({
      goodsId: options.goods_id
    });

    $wx.setNavColor(getColor().val);
    this.getGoodsDetail();
  },

  onShareAppMessage(){

  },

  //获取用户信息
  userInfo(){
    
  },

  // 收藏
  collect(e){
    this.setData({
      isCollectActive: true,
      collectNumColor: this.randomColor()
    });
    this.like();
  },

  // 监听收藏动画结束
  collcetAnimationend(){
    this.setData({
      isCollectActive: false
    });

  },

  // 随机色
  randomColor(){
    let r = Math.floor(Math.random() * 256),
        g = Math.floor(Math.random() * 256),
        b = Math.floor(Math.random() * 256);

    return  'color:rgb(' + r + ',' + g + ',' + b + ')'
  },

  // 查看商品图
  viewCommodityImgs(e){
    let index = e.currentTarget.dataset.index,
        imgs = e.currentTarget.dataset.imgs;

    wx.previewImage({
      current: imgs[index],
      urls: imgs,
      fail(res){
        $wx.showToast(res.msg);
      }
    });
  },

  /**
   * 商品详情
   * @param goods_id
   * */
  getGoodsDetail(goodsId){
    let _this = this, td = _this.data;
    wx.showNavigationBarLoading();
    
    AJAX.getGoodsDetail({
      goods_id: td.goodsId
    },(status, data, msg)=>{
      wx.hideNavigationBarLoading();
      if(status){
        _this.setData({
          goodInfo: data || {}
        });
      }else{
        $wx.showToast(msg);
      }
    });
  },

  /**
   * 点赞
   * @param good_id
   * */ 
   like(){
     let _this = this, td = _this.data;
     
     AJAX.like({
       goods_id: td.goodsId
     },(status, data, msg)=>{
       if(status){
         _this.getGoodsDetail();
       }else{
         $wx.showToast(msg);
       }
     });
   }
  
});